import { requestPost } from '../../assets/js/request.js';
import Notify from '@vant/weapp/notify/notify';
Page({
  formSubmit:function(e){
    if(this.data.status === 'Student'){
      if (e.detail.value.uName != null && e.detail.value.uId != null && e.detail.value.pwd1 != null && e.detail.value.pwd2 != null && e.detail.value.classId != null) {
        if (e.detail.value.pwd1 === e.detail.value.pwd2) {
          if (e.detail.value.pwd1.indexOf(" ") == -1) {
            wx.showLoading({
              title: '正在注册',
              mask: true,
            })
            let reqData = {
              status: e.detail.value.status,
              uId: e.detail.value.uId,
              uName: e.detail.value.uName,
              uPwd: e.detail.value.pwd1,
              classId: e.detail.value.classId,
              uSex: e.detail.value.sex,
            }
            requestPost('/userRegister.php', reqData)
              .then((res)=>{
                if (res.data == 'ok') {
                  wx.showToast({
                    title: '注册成功',
                    icon: 'success',
                  })
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 1500)
                } else if (res.data == 'repate') {
                  Notify({ type: 'danger', message: '注册失败，该账号已存在' });
                } else if (res.data == 'noclass') {
                  Notify({ type: 'danger', message: '注册失败，班级编号不存在' });
                } else if (res.data == 'fail') {
                  Notify({ type: 'danger', message: '注册失败' });
                }
              })
              .catch((err)=>{
                Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
              })
          } else {
            Notify({ type: 'danger', message: '密码中不能有空格' });
          }
        } else {
          Notify({ type: 'danger', message: '两次密码输入不一致' });
        }
      } else {
        Notify({ type: 'danger', message: '请填写完整信息' });
      }
    }else if(this.data.status === 'Teacher'){
      if (e.detail.value.uName != null && e.detail.value.uId != null && e.detail.value.pwd1 != null && e.detail.value.pwd2 != null && e.detail.value.email != null) {
        if (e.detail.value.pwd1 === e.detail.value.pwd2) {
          if (e.detail.value.pwd1.indexOf(" ") == -1) {
            wx.showLoading({
              title: '正在注册',
              mask: true,
            })
            let regData = {
              status: e.detail.value.status,
              uId: e.detail.value.uId,
              uName: e.detail.value.uName,
              uPwd: e.detail.value.pwd1,
              email: e.detail.value.email,
              uSex: e.detail.value.sex,
            }
            requestPost('/userRegister.php', regData)
              .then((res)=>{
                if (res.data == 'ok') {
                  wx.showToast({
                    title: '注册成功',
                    icon: 'success'
                  })
                  setTimeout(function(){
                    wx.navigateBack({
                      delta: 1
                    })
                  },1500)
                } else if (res.data == 'repate') {
                  Notify({ type: 'danger', message: '注册失败，该账号已存在' });
                } else if (res.data == 'fail') {
                  Notify({ type: 'danger', message: '注册失败' });
                }
              })
              .catch((err)=>{
                Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
              })
          } else {
            Notify({ type: 'danger', message: '密码中不能有空格' });
          }
        } else {
          Notify({ type: 'danger', message: '两次密码输入不一致' });
        }
      } else {
        Notify({ type: 'danger', message: '请填写完整信息' });
      }
    }
  },
  click:function(){
    this.setData({
      show: !this.data.show
    })
  },
  onChange(event) {
    this.setData({
      radio: event.detail
    });
  },

  /**
   * 页面的初始数据
   */
  data: {
    radio: '1',
    show: false,
    status:'Student',
    actions: [
      {
        name: 'Teacher'
      },
      {
        name: 'Student'
      }
    ]
  },
  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    this.setData({
      status: event.detail.name
    })
  }
})